//
//  ListCell.m
//  MyCarsCanFly
//
//  Created by 何东东 on 15/9/23.
//  Copyright © 2015年 何东东. All rights reserved.
//

#import "ListCell.h"
//#import "BaseHeader.pch"
#import "ListModel.h"
#import <SDWebImage/UIImageView+WebCache.h>
//#import "UIImageView+WebCache.h"
#define WIDTH self.contentView.frame.size.width
#define HEIGHT self.contentView.frame.size.height


@interface ListCell ()

@property (nonatomic, retain) UIImageView *picImage;
@property (nonatomic, retain) UILabel *titleLable;
@property (nonatomic, retain) UILabel *timeLable;
@property (nonatomic, retain) UILabel *placeLable;
@property (nonatomic, retain) UIButton *button1;
@property (nonatomic, retain) UIButton *button2;
@property (nonatomic, retain) UIImageView *Image1;
@property (nonatomic, retain) UIImageView *Image2;
@property (nonatomic, retain) UIImageView *Image3;
@property (nonatomic, retain) UIImageView *Image4;
@property (nonatomic, retain) UILabel *luntanLable;
@property (nonatomic, assign) NSInteger add;
@property (nonatomic, assign) NSInteger intering;
@property (nonatomic, assign) BOOL isNight;

@end

@implementation ListCell


/* 判定当前的模式是否是夜间模式 */
- (void)nightCheck {
    NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    
    NSString *nightPath = [documentPath stringByAppendingPathComponent:@"night.txt"];
    
    NSError *error = nil;
    
    NSString *nightModel = [NSString stringWithContentsOfFile:nightPath encoding:NSUTF8StringEncoding error:&error];
    
    if ([nightModel isEqualToString:@"YES"]) {
        self.isNight = YES;
    } else {
        self.isNight = NO;
    }
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
//        [self nightCheck];
        self.isNight = YES;
        self.picImage = [[UIImageView alloc] init];
        [self.contentView addSubview:self.picImage];
 
        
        self.titleLable = [[UILabel alloc] init];
        [self.contentView addSubview:self.titleLable];
        
        
        self.timeLable = [[UILabel alloc] init];
        [self.contentView addSubview:self.timeLable];
        self.timeLable.textColor = [UIColor grayColor];
        self.timeLable.font = [UIFont systemFontOfSize:15];

        
        self.placeLable = [[UILabel alloc] init];
        [self.contentView addSubview:self.placeLable];
        self.placeLable.textColor = [UIColor grayColor];
        self.placeLable.font = [UIFont systemFontOfSize:15];
    
        
        self.button1 = [UIButton buttonWithType:UIButtonTypeCustom];
        self.button2 = [UIButton buttonWithType:UIButtonTypeCustom];
        self.button2.layer.borderWidth = 0.5;
        self.button2.layer.borderColor = [[UIColor grayColor] CGColor];
        self.button2.titleLabel.font = [UIFont systemFontOfSize:12];
        [self.button2 setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [self.button2 addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];

        
        self.button1.layer.borderWidth = 0.5;
        self.button1.layer.borderColor = [[UIColor grayColor] CGColor];
        self.button1.titleLabel.font = [UIFont systemFontOfSize:12];
        [self.button1 setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];

        [self.contentView addSubview:self.button1];
        [self.contentView addSubview:self.button2];
        
        self.Image1 = [[UIImageView alloc] init];
        [self.button1 addSubview:self.Image1];
      
        self.Image2 = [[UIImageView alloc] init];
        [self.button2 addSubview:self.Image2];
      

        self.Image3 = [[UIImageView alloc] init];
        [self.picImage addSubview:self.Image3];
 
        self.Image4 = [[UIImageView alloc] init];
        [self.picImage addSubview:self.Image4];


        self.luntanLable = [[UILabel alloc] init];
        [self.Image4 addSubview:self.luntanLable];
        self.luntanLable.font = [UIFont systemFontOfSize:12];
        self.luntanLable.textColor = [UIColor whiteColor];

        
        
        if (self.isNight) {
            self.contentView.backgroundColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:1];
            self.titleLable.textColor = [UIColor blackColor];
            
        } else {
            self.contentView.backgroundColor = [UIColor colorWithRed:(CGFloat)34 / 255 green:(CGFloat)47 / 255 blue:(CGFloat)63 / 255 alpha:1.000];
            self.titleLable.textColor = [UIColor lightGrayColor];

        }
        /* 夜间模式切换 */
        [[NSNotificationCenter defaultCenter] addObserverForName:@"night" object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *note) {
//            NSString *string = [note.userInfo objectForKey:@"night"];
//            if ([string isEqualToString:@"YES"]) {
//                self.contentView.backgroundColor = [UIColor colorWithRed:(CGFloat)34 / 255 green:(CGFloat)47 / 255 blue:(CGFloat)63 / 255 alpha:1.000];
//                self.titleLable.textColor = [UIColor lightGrayColor];
//            } else {
                self.contentView.backgroundColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:1];
                self.titleLable.textColor = [UIColor blackColor];


//            }
        }];
    }
    
    return self;
}

- (void)layoutSubviews {
    
    [super layoutSubviews];
    
    self.picImage.frame = CGRectMake(WIDTH * 0.027, WIDTH * 0.027, WIDTH - WIDTH * 0.053, HEIGHT / 2 + WIDTH * 0.08);
    self.titleLable.frame = CGRectMake(WIDTH * 0.053, WIDTH * 0.52, WIDTH, WIDTH * 0.08);
    self.timeLable.frame = CGRectMake(WIDTH * 0.053, WIDTH * 0.6, WIDTH * 0.8, WIDTH * 0.053);
    self.placeLable.frame = CGRectMake(WIDTH * 0.053, WIDTH * 0.65, WIDTH * 0.8, WIDTH * 0.053);
    self.button1.frame = CGRectMake(WIDTH * 0.027, WIDTH * 0.71, WIDTH * 0.47, WIDTH * 0.09);
    self.button2.frame = CGRectMake(WIDTH * 0.5, WIDTH * 0.71, WIDTH * 0.47, WIDTH * 0.09);
    self.Image1.frame = CGRectMake(WIDTH * 0.053, WIDTH * 0.02, WIDTH * 0.053, WIDTH * 0.053);
    self.Image2.frame = CGRectMake(WIDTH * 0.053, WIDTH * 0.02, WIDTH * 0.053, WIDTH * 0.053);
    self.Image3.frame = CGRectMake(0, 0, WIDTH * 0.11, WIDTH * 0.11);
    self.Image4.frame = CGRectMake(WIDTH * 0.8, WIDTH * 0.43, WIDTH * 0.16, WIDTH * 0.067);
    self.luntanLable.frame = CGRectMake(WIDTH * 0.013, 0, WIDTH * 0.13, WIDTH * 0.053);

}


- (void)setModel:(ListModel *)model {
    
    if (_model != model) {
        _model = model;
    }
    
    NSURL *url = [NSURL URLWithString:model.eventBigImg];
    [self.picImage sd_setImageWithURL:url];
    
    self.titleLable.text = model.eventTitle;
    NSString*str = [NSString stringWithFormat:@"活动截止时间: %@", model.eventEndTime];
    self.timeLable.text = str;
    
    NSString *str1 = [NSString stringWithFormat:@"活动地点: %@", model.eventPlace];
    self.placeLable.text = str1;
    
    [self.button2 setTitle:@"分享给好友" forState:UIControlStateNormal];
    self.intering = model.eventInterest;
    NSString *str3 = [NSString stringWithFormat:@"%ld人感兴趣", self.intering];
    [self.button1 setTitle:str3 forState:UIControlStateNormal];
    [self.button1 addTarget:self action:@selector(add:) forControlEvents:UIControlEventTouchUpInside];

    self.Image1.image = [UIImage imageNamed:@"zan"];
    self.Image2.image = [UIImage imageNamed:@"fenxiang"];
    self.Image3.image = [UIImage imageNamed:@"jinxing"];
    self.Image4.image = [UIImage imageNamed:@"luntan"];
    self.luntanLable.text = @"论坛活动";
    
}
- (void)add:(UIButton *)add {
    
    self.intering ++;
    NSString *str3 = [NSString stringWithFormat:@"%ld人感兴趣", self.intering];
    [self.button1 setTitle:str3 forState:UIControlStateNormal];

    
}

- (void)click:(UIButton *)click {
    
    [self.delegate fenxiang];
    
}


- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
